﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autofac;
using Microsoft.Practices.ServiceLocation;

namespace NQueries.TestSupport
{
    public class AutofacServiceLocator : ServiceLocatorImplBase
    {
        readonly AutofacContrib.CommonServiceLocator.AutofacServiceLocator _ServiceLocator;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="container"></param>
        [DebuggerStepThrough]
        public AutofacServiceLocator(IContainer container)
        {
            Container = container;
            _ServiceLocator = new AutofacContrib.CommonServiceLocator.AutofacServiceLocator(container);
        }

        /// <inheritdoc />
        public IContainer Container { get; private set; }

        /// <summary>
        /// Gets all instances of that type.
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        [DebuggerStepThrough]
        protected override IEnumerable<object> DoGetAllInstances(Type serviceType) { return _ServiceLocator.GetAllInstances(serviceType); }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [DebuggerStepThrough]
        protected override object DoGetInstance(Type serviceType, string key) { return _ServiceLocator.GetInstance(serviceType, key); }
    }
}